<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <style>
        #link {
            /* display: none; */
            transform: rotate(90deg);
            position: absolute;
            top: 38%;
            left: 15%;
        }

        #temp {
            display: none;
            transform: rotate(90deg);
            position: absolute;
            top: 48%;
            left: 15%;
        }

        #person {
            display: none;
            transform: rotate(90deg);
            position: absolute;
            top: 50%;
            left: 0%;
        }
        #poker-list {
            position: absolute;
            top: -98px;
            left: -270px;
            width: 560px;
            height: 80px;
        }

        #button-list {
            display: none;
            overflow: hidden;
            position: absolute;
            top: -149px;
            left: -318px;
            width: 480px;
            height: 80px;
        }

        #button-lead {
            display: none;
            overflow: hidden;
            position: absolute;
            top: -149px;
            left: -318px;
            width: 480px;
            height: 80px;
        }

        #userInfo {
            overflow: hidden;
            position: absolute;
            top: -147px;
            left: -25px;
            width: 80px;
            height: 30px;
        }

        #left {
            overflow: hidden;
            position: absolute;
            top: -218px;
            left: -265px;
            width: 120px;
            height: 30px;
        }

        #right {
            overflow: hidden;
            position: absolute;
            top: -217px;
            left: 225px;
            width: 120px;
            height: 30px;
        }

        #current-list {
            overflow: hidden;
            position: absolute;
            top: -269px;
            left: -68px;
            width: 480px;
            height: 80px;
        }

        #bottom-list {
            overflow: hidden;
            position: absolute;
            top: -359px;
            left: -318px;
            width: 480px;
            height: 80px;
        }

      

        .poker {
            width: 25px;
            height: 80px;
            padding: 1px;
            margin-left: 1px;
            margin-top: 1px;
            color: white;
            background-color: green;
            float: left;
            text-align: center;
        }
        .button {
            margin-left: 174px;
        }
    </style>
</head>

<body>
    <table id="link">
        <tr>
            <td>局域网IP：</td>
            <td><input id="ip" value = "192.168.1.216"></td>
        </tr>
        <tr>
            <td>房间ID：</td>
            <td><input id="roomid" value = "1"></td>
        </tr>
        
        <tr>
            <td></td>
            <td><button onclick="link()">连接</button></td>
        </tr>
    </table>
    <div id="temp">连接成功，等待其他玩家连接。。。</div>
    <div id="person">
        <div id="userInfo"></div>
        <div id="left"></div>
        <div id="right"></div>
        <div id="button-list">
            <button class="button" onclick="noLaunch()">不出</button>
            <button class="button" onclick="launch()">出牌</button>
        </div>
        <div id="button-lead">
            <button class="button" onclick="noGrab()">不抢</button>
            <button class="button" onclick="grab()">抢地主</button>
        </div>
        <div id="poker-list">
            <!-- <div class="poker">12</div> -->
        </div>
        <div id="current-list">
            <!-- <div class="poker">12</div> -->
        </div>
        <div id="bottom-list">
            <div class="poker"></div>
            <div class="poker"></div>
            <div class="poker"></div>
        </div>
    </div>


    <script>
        var webSocket = null;
        var ip = null;
        var sid = null;
        var roomid = null;
        var pokerList = [];
        var count = 1;
        var tempBottom = null;
        var crrentPoker = [];
        var grabFlag = false;
        // var launchFlag = 0;
        var beforePoker = null;
        var self = null;
        document.getElementById("roomid").value = new Date().getHours()+""+new Date().getMinutes();
        var timeInterver = null;
        function link() {
            ip = document.getElementById("ip").value;
            sid = Math.round(Math.random()*100000)+"321";
            roomid = document.getElementById("roomid").value;
            // if (document.getElementById("left").innerHTML.substring(0,2))
            // userName = document.getElementById("userName").value;
            // document.getElementById("userInfo").innerHTML = userName;
            console.log(sid)

            if ('WebSocket' in window) {
                // console.log("WebSocket")
                webSocket = new WebSocket("ws://" + ip + ":9000/api/websocket/" + roomid + "/" + sid);
            } else {
                alert("当前浏览器 Not support webSocket");
            }




            //连接发送错误回调方法
            webSocket.onerror = function () {
                
            }

            //连接成功建立回调方法
            webSocket.onopen = function (e) {
                // console.log(e);
                // console.log("onopen"+e);
            }

            //监听服务器发送的消息
            webSocket.onmessage = function (e) {
                // console.log("收到消息");
                // console.log(e.data);
                if (e.data[0] === 'P') {
                    parsePoker(e.data);
                }
                if (e.data[0] === 'W') {
                    win(e.data)
                }
            }

            var U01data, Uiddata, Usdata;

            //连接关闭回调方法
            webSocket.onclose = function () {
                console.log("连接关闭")
                console.log("尝试重连")
                reconnecting()
            }

            //监听窗口关闭事件
            window.onbeforeunload = function () {

            }
            document.getElementById("temp").style.display = "block";
            document.getElementById("link").style.display = "none";
            timeInterver = setInterval(function(){ webSocket.send("T"+sid); }, 20000);
        }
        
        function reconnecting() {
            clearInterval(timeInterver)
            webSocket = new WebSocket("ws://" + ip + ":9000/api/websocket/" + roomid + "/" + sid);
                //监听服务器发送的消息
            webSocket.onmessage = function (e) {
                // console.log("收到消息");
                // console.log(e.data);
                if (e.data[0] === 'P') {
                    parsePoker(e.data);
                }
                if (e.data[0] === 'W') {
                    win(e.data)
                }
            }
             //连接关闭回调方法
             webSocket.onclose = function () {
                console.log("连接关闭")
                console.log("尝试重连")
                reconnecting()
            }
            timeInterver = setInterval(function(){ webSocket.send("T"+sid); }, 5000);
        }
        
        function win(s) {
            console.log("运行")
            s = s.substring(1);
            s = s.substring(0,s.lastIndexOf("321"));
            document.getElementById("current-list").innerHTML ="id："+s+" 赢了<button class='button' onclick='reset()'>再来一把</button>";
            document.getElementById("button-list").style.display = "none";
            pokerList = [];
            count = 1;
            tempBottom = null;
            crrentPoker = [];
            grabFlag = false;
            // launchFlag = 0;
            beforePoker = null;
        }

        function reset() {
            message = "R"+sid;
            webSocket.send(message);
            document.getElementById("temp").style.display = "block";
            document.getElementById("current-list").innerHTML = "";
        }

        // 出牌
        function launch() {
            console.log("出牌")
            // console.log(pokerList)
            // console.log(crrentPoker)
            // console.log(self)
            let  flag = true;
            if (self !== null) {
                if (self.length === crrentPoker.length) {
                    let arr = [];
                    for (let i = 0; i < crrentPoker.length;i++) {
                        arr[i] = parseInt(crrentPoker[i]);
                    }
                    self = sort(self)
                    arr = sort(arr)
                    console.log(arr)
                    console.log(self)
                    let flag1 = false
                    for (let i = 0; i  < self.length; i++) {
                        if (parseInt(self[i]) !== parseInt(arr[i])) {
                            flag1 = true;
                        }
                    }
                    if (flag1 === false) {
                        flag = false
                    }
                }
                
            }
            all = document.querySelector("div#poker-list").children;
            for (var i = 0; i < all.length; i++) {
                if (all[i].style.cssText === "background-color: red;") {
                    pokerList.push(reverseMapping(all[i].innerText))
                }
            }
            
            // console.log(pokerList)
            // console.log(crrentPoker)
            if (!flag || judge(pokerList, crrentPoker) ) {
                var message = "L"+roomid+","+sid+",";
                for (var i = 0; i < pokerList.length; i++) {
                    message += pokerList[i]+",";
                }
                webSocket.send(message);
            }
            self = pokerList;
            pokerList=[];
        }

        function judge(pokerList, crrentPoker) {
            let type1 = commonJudge(pokerList);
            let type2;
            if (type1 == 0) {
                alert("您出的牌不合法");
                return false;
            } 
            if (crrentPoker[0] === '') {
                return type1;
            } else {
                // if (launchFlag == 2) {
                //     return true;
                // }

                // 3 王炸 12炸弹
                if (type1 === 3) {
                    return true;
                }
                type2 = commonJudge(crrentPoker);
                if (type1 === 12 && type2 !== 12) {
                    return true;
                }
                if (type1 != type2) {
                    alert("您出的牌不合法");
                    return false;
                } else {
                    if (compareJudge(type1,pokerList,crrentPoker)) {
                        return true;
                    }
                }
            }
        
            alert("您出的牌不合法");
            return false;
        }

        function commonJudge(pokerList) {
            if(single(pokerList)) {
                return 1;
            } else if(double(pokerList)) {
                return 2;
            } else if(judgeKingDouble(pokerList)) {
                return 3;
            } else if(judge3TheSame(pokerList)) {
                return 4;
            } else if(judge3TheSameWithOne(pokerList)) {
                return 5;
            } else if(judge3TheSameWithDouble(pokerList)) {
                return 6;
            } else if(judgeContinuous(pokerList)) {
                return 7;
            } else if(judgeContinuousDouble(pokerList)) {
                return 8;
            } else if(airplane6Plus(pokerList)) {
                return 9;
            } else if(airplane8Plus(pokerList)) {
                return 10;
            } else if(airplane10Plus(pokerList)) {
                return 11;
            } else if(judge4TheSame(pokerList)) {
                return 12;
            } else if(judge4TheSameWithTwo(pokerList)) {
                return 13;
            } else if(judge4TheSameWithTwoPair(pokerList)) {
                return 14;
            } 
            return 0;
        }

        function compareJudge(type,pokerList,crrentPoker) {
            
            if(type == 1) {
                return singleCompare(pokerList,crrentPoker);
            } else if(type == 2) {
                return doubleCompare(pokerList,crrentPoker);
            } else if(type == 3) {
                return judgeKingDoubleCompare(pokerList,crrentPoker);
            } else if(type == 4) {
                return judge3TheSameCompare(pokerList,crrentPoker);
            } else if(type == 5) {
                return judge3TheSameWithOneCompare(pokerList,crrentPoker);
            } else if(type == 6) {
                return judge3TheSameWithDoubleCompare(pokerList,crrentPoker);
            } else if(type == 7) {
                return judgeContinuousCompare(pokerList,crrentPoker);
            } else if(type == 8) {
                return judgeContinuousDoubleCompare(pokerList,crrentPoker);
            } else if(type == 9) {
                return airplane6PlusCompare(pokerList,crrentPoker);
            } else if(type == 10) {
                return airplane8PlusCompare(pokerList,crrentPoker);
            } else if(type == 11) {
                return airplane10PlusCompare(pokerList,crrentPoker);
            } else if(type == 12) {
                return judge4TheSameCompare(pokerList,crrentPoker);
            } else if(type == 13) {
                return judge4TheSameWithTwoCompare(pokerList,crrentPoker);
            } else if(type == 14) {
                return judge4TheSameWithTwoPairCompare(pokerList,crrentPoker);
            }
            return 0;
        }

       
        
        // 单张
        function single(pokerList) {
            if (pokerList.length === 1) {
                return true
            }
            return false;
        }

         // 单张比较
        function singleCompare(pokerList, crrentPoker) {
            if (pokerList[0] > crrentPoker[0]) {
                return true
            }
            return false;
        }

        // 一对
        function double(pokerList) {
            if ((pokerList.length === 2) && (parseInt(pokerList[0]) === parseInt(pokerList[1]))) {
                return true
            }
            return false;
        }

         // 一对比较
        function doubleCompare(pokerList, crrentPoker) {
            if (pokerList[0] > crrentPoker[0]) {
                return true
            }
            return false;
        }

        //王炸
        function judgeKingDouble(pokerList) {
            if ((parseInt(pokerList[0]) === 15 && parseInt(pokerList[1]) === 14) || (parseInt(pokerList[0]) === 14 && parseInt(pokerList[1]) === 15)) {
                return true
            }
            return false;
        }
        
        // 三张相同
        function judge3TheSame(pokerList) {
            if (pokerList.length !== 3) return false;
            if ((parseInt(pokerList[0]) === parseInt(pokerList[1])) && (parseInt(pokerList[1]) === parseInt(pokerList[2]))) {
                return true
            }
            return false;
        }

        // 三张相同 比较
        function judge3TheSameCompare(pokerList, crrentPoker) {
            if (pokerList[0] > crrentPoker[0]) {
                return true
            }
            return false;
        }

        // 三带一
        function judge3TheSameWithOne(pokerList) {
            if (pokerList.length !== 4) return false;
            let obj = countPoker(pokerList);
            arr1 = airplaneNumber3(obj);
            arr2 = airplaneNumber1(obj);
            if (arr1.length * 3 + arr2.length * 1 === pokerList.length) {
                return true;
            }
            return false;
        }

        // 三带一 比较
        function judge3TheSameCompare(pokerList, crrentPoker) {
            let obj1 = countPoker(pokerList);
            arr1 = airplaneNumber3(obj1);

            let obj2 = countPoker(crrentPoker);
            arr2 = airplaneNumber3(obj2);

            if (parseInt(arr1[0]) > parseInt(arr2[0])) {
                return true
            }
            return false;
        }

        // 三带一对
        function judge3TheSameWithDouble(pokerList) {
            if (pokerList.length !== 5) return false;
            let obj = countPoker(pokerList);
            arr1 = airplaneNumber3(obj);
            arr2 = airplaneNumber2(obj);
            if (arr1.length * 3 + arr2.length * 2 === pokerList.length) {
                return true;
            }
            return false;
        }

        // 三带一对 比较
        function judge3TheSameWithDoubleCompare(pokerList, crrentPoker) {
            let obj1 = countPoker(pokerList);
            arr1 = airplaneNumber3(obj1);

            let obj2 = countPoker(crrentPoker);
            arr2 = airplaneNumber3(obj2);

            if (parseInt(arr1[0]) > parseInt(arr2[0])) {
                return true
            }
            return false;
        }

        // 顺子
        function judgeContinuous(pokerList) {
            if (pokerList.length < 5) return false;
            if (pokerList.length > 13) return false;
            let arr = [];
            for (let i = 0; i < pokerList.length; i++) {
                arr[i] = parseInt(pokerList[i]);
            }
            arr = sort(arr);

            let pre = arr[0];
            for (let i = 1; i < arr.length; i++) {
                if (arr[i] !== (pre + 1)) {
                    return false;
                }
                pre = arr[i];
            }
            return true;
        }

        // 顺子 比较
        function judgeContinuousCompare(pokerList, crrentPoker) {   
            if (pokerList.length !== crrentPoker.length) {
                // alert("您出的牌不合法");
                return false;
            }
            pokerList = sort(pokerList)
            crrentPoker = sort(crrentPoker)
            if (pokerList[0] > crrentPoker[0]) {
                return true
            }
            return false;
        }

        // 连对
        function judgeContinuousDouble(pokerList) {
            if (pokerList.length < 6) return false;
            let obj = countPoker(pokerList);
            if(!airplaneContinuous2(obj)) {
                // console.log("运行")
                return false;
            }
            arr = airplaneNumber2(obj);
            
            if (arr.length * 2 === pokerList.length) {
                return true;
            }
            return false;
        }

        // 连对 比较
        function judgeContinuousCompare(pokerList, crrentPoker) {
            if (pokerList.length !== crrentPoker.length) {
                return false;
            }
            pokerList = sort(pokerList)
            crrentPoker = sort(crrentPoker)
            if (pokerList[0] > crrentPoker[0]) {
                return true
            }
            return false;
        }
        
        // 飞机不带东西
        function airplane6Plus(pokerList) {
            if (pokerList.length < 6) return false;
            let obj = countPoker(pokerList);
            if(!airplaneContinuous3(obj)) {
                // console.log("运行")
                return false;
            }

            // 获取3张相同牌的数组
            arr = airplaneNumber3(obj); 
            // console.log(arr)
            if (arr.length * 3 === pokerList.length) {
                return true;
            }
            return false;
        }

        // 飞机不带东西 比较
        function airplane6PlusCompare(pokerList, crrentPoker) {
            if (pokerList.length !== crrentPoker.length) {
                return false;
            }
            let obj1 = countPoker(pokerList);
            arr1 = airplaneNumber3(obj1);

            let obj2 = countPoker(crrentPoker);
            arr2 = airplaneNumber3(obj2);

            arr1 = sort(arr1)
            arr2 = sort(arr2)
            if (arr1[0] > arr2[0]) {
                return true
            }
            return false;
        }

        // 飞机带一个
        function airplane8Plus(pokerList) {
            if (pokerList.length < 8) return false;
            let obj = countPoker(pokerList);
            if(!airplaneContinuous3(obj)) {
                // console.log("运行")
                return false;
            }
            
            // 获取3张相同牌的数组
            arr = airplaneNumber3(obj); 
            // console.log(arr)
            if (arr.length * 4 === pokerList.length || arr.length * 3 === pokerList.length) {
                return true;
            }
            return false;
        }

        // 飞机带一个 比较
        function airplane8PlusCompare(pokerList, crrentPoker) {
            if (pokerList.length !== crrentPoker.length) {
                return false;
            }
            let obj1 = countPoker(pokerList);
            arr1 = airplaneNumber3(obj1);

            let obj2 = countPoker(crrentPoker);
            arr2 = airplaneNumber3(obj2);

            arr1 = sort(arr1)
            arr2 = sort(arr2)
            if (arr1[0] > arr2[0]) {
                return true
            }
            return false;
        }

        // 飞机带两个
        function airplane10Plus(pokerList) {
            if (pokerList.length < 10) return false;
            let obj = countPoker(pokerList);
            if(!airplaneContinuous3(obj)) {
                return false;
            }
            
            // 获取3张相同牌的数组
            arr1 = airplaneNumber3(obj);
            arr2 = airplaneNumber2(obj);
            // console.log(arr)
            if (arr1.length * 3 + arr1.length * 2 === pokerList.length) {
                return true;
            }
            return false;
        }


        // 飞机带两个 比较
        function airplane10PlusCompare(pokerList, crrentPoker) {
            if (pokerList.length !== crrentPoker.length) {
                return false;
            }
            let obj1 = countPoker(pokerList);
            arr1 = airplaneNumber3(obj1);

            let obj2 = countPoker(crrentPoker);
            arr2 = airplaneNumber3(obj2);

            arr1 = sort(arr1)
            arr2 = sort(arr2)
            if (arr1[0] > arr2[0]) {
                return true
            }
            return false;
        }

        // 四张相同 炸弹
        function judge4TheSame(pokerList) {
            if (pokerList.length !== 4) return false;
            if ((parseInt(pokerList[0]) === parseInt(pokerList[1])) && (parseInt(pokerList[1]) === parseInt(pokerList[2])) && (parseInt(pokerList[2]) === parseInt(pokerList[3]))) {
                return true
            }
            return false;
        }

         // 炸弹 比较
        function judge4TheSameCompare(pokerList, crrentPoker) {
            if (parseInt(pokerList[0]) > parseInt(crrentPoker[0])) {
                return true
            }
            return false;
        }

        // 四带二
        function judge4TheSameWithTwo(pokerList) {
            
            if (pokerList.length !== 6) return false;
            
            let obj = countPoker(pokerList);
            arr1 = airplaneNumber4(obj);
            arr2 = airplaneNumber2(obj);
            arr3 = airplaneNumber1(obj);
           
            if ((arr1.length * 4 + arr2.length * 2 === pokerList.length) || (arr1.length * 4 + arr3.length * 1 === pokerList.length)) {
                return true;
            }
            return false;
        }

         // 四带二 比较
         function judge4TheSameWithTwoCompare(pokerList, crrentPoker) {
            let obj1 = countPoker(pokerList);
            arr1 = airplaneNumber4(obj1);

            let obj2 = countPoker(crrentPoker);
            arr2 = airplaneNumber4(obj2);
            if (parseInt(arr1[0]) > parseInt(arr2[0])) {
                return true
            }
            return false;
        }

        // 四带两对
        function judge4TheSameWithTwoPair(pokerList) {
            if (pokerList.length !== 8) return false;
            let obj = countPoker(pokerList);
            arr1 = airplaneNumber4(obj);
            arr2 = airplaneNumber2(obj);
            if (arr1.length * 4 + arr2.length * 2 === pokerList.length) {
                return true;
            }
            return false;
        }


        // 四带二 比较
        function judge4TheSameWithTwoPairCompare(pokerList, crrentPoker) {
            let obj1 = countPoker(pokerList);
            arr1 = airplaneNumber4(obj1);

            let obj2 = countPoker(crrentPoker);
            arr2 = airplaneNumber4(obj2);
            if (parseInt(arr1[0]) > parseInt(arr2[0])) {
                return true;
            }
            return false;
        }

       

        

        function sort(arr) {
            for (let i = 0; i < arr.length; i++) {
                for (let j = 0; j <arr.length-i; j++) {
                    if (arr[j] > arr[j+1]) {
                        let temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;
                    }
                }
            }
            return arr;
        }
        function increment(arr) {
            let pre = arr[0];
            for (let i = 1; i < arr.length; i++) {
                if (parseInt(arr[i]) !== parseInt((pre + 1))) {
                    return false;
                }
                pre = arr[i];
            }
            return true;
        }

        function airplaneContinuous3(obj) {
            // 飞机连续
            arr = airplaneNumber3(obj);
            arr = sort(arr);
            if (arr.length === 4) {
                if (arr[1] != arr[0] + 1) {
                    arr.splice(1);
                }
                if (arr[arr.length-1] != arr[arr.length-2] + 1) {
                    arr.splice(0,arr.length-1)
                }
            }
            if (!increment(arr)) {
                return false;
            }
            return true;
        }

        function airplaneContinuous2(obj) {
            // 飞机连续
            arr = airplaneNumber2(obj);
            arr = sort(arr);
            if (!increment(arr)) {
                return false;
            }
            return true;
        }
        function airplaneNumber3(obj) {
            let arr = [];
            let k = 0;
            for (let key in obj) {
                if (parseInt(obj[key]) === 3) {
                    arr[k] = parseInt(key);
                    k++;
                }
            }
            return arr;
        }
        function airplaneNumber2(obj) {
            let arr = [];
            let k = 0;
            for (let key in obj) {
                if (parseInt(obj[key]) === 2) {
                    arr[k] = parseInt(key);
                    k++;
                }
            }
            return arr;
        }
        function airplaneNumber1(obj) {
            let arr = [];
            let k = 0;
            for (let key in obj) {
                if (parseInt(obj[key]) === 1) {
                    arr[k] = parseInt(key);
                    k++;
                }
            }
            return arr;
        }
        function airplaneNumber4(obj) {
            let arr = [];
            let k = 0;
            for (let key in obj) {
                if (parseInt(obj[key]) === 4) {
                    arr[k] = parseInt(key);
                    k++;
                }
            }
            return arr;
        }
        function countPoker(pokerList) {
            let obj = {};
            for (let i = 0; i < pokerList.length; i++) {
                if (obj[pokerList[i]] === undefined) {
                    obj[pokerList[i]] = 1;
                } else {
                    obj[pokerList[i]] += 1;
                }
            }
            return obj;
        }

        

        

        


        // 不出牌
        function noLaunch() {
            var message = "l"+roomid+","+sid+",";
            // for (var i = 0; i < pokerList.length; i++) {
            //     message += pokerList[i]+",";
            // }
            webSocket.send(message);
        }

        
        function noGrab() {
            var message = "q"+roomid+","+sid+",";
            // for (var i = 0; i < pokerList.length; i++) {
            //     message += pokerList[i]+",";
            // }
            webSocket.send(message);
            count = 2;
        }

        function grab() {
            var message = "Q"+roomid+","+sid+",";
            // for (var i = 0; i < pokerList.length; i++) {
            //     message += pokerList[i]+",";
            // }
            webSocket.send(message);
            count = 2;
        }



        //将消息显示在网页上
        function setMessageInnerHTML(innerHTML) {
            
            
        }

        // 关闭WebSocket连接
        function closeWebSocket() {
            webSocket.close();
        }

        // 发送消息
        function send() {
            // var message = document.getElementById('text').value;
            
            webSocket.send('{"msg":"' + message + '"}');
        }

        function sendMessage() {
            var message = document.getElementById("input").value;
            $.ajax({
                url: "http://localhost:9000/api/socket/socket/push/99?message=" + message,
                type: "GET",
                success: function (data) {
                    // setMessageInnerHTML()
                }
            })
        }

        function parsePoker(s) {
            s = s.substring(s.indexOf("map={")+5, s.indexOf("}}"))+',';
            list = s.split('],');
            person1 = {
                id: list[0].trim().substring(0, list[0].trim().indexOf("=[")),
                poker: list[0].trim().substring(list[0].trim().indexOf("=[")+2).split(", ")
            }
            person2 = {
                id: list[1].trim().substring(0, list[1].trim().indexOf("=[")),
                poker: list[1].trim().substring(list[1].trim().indexOf("=[")+2).split(", ")
            }
            person3 = {
                id: list[2].trim().substring(0, list[2].trim().indexOf("=[")),
                poker: list[2].trim().substring(list[2].trim().indexOf("=[")+2).split(", ")
            }

            person4 = {
                id: list[3].trim().substring(0, list[3].trim().indexOf("=[")),
                poker: list[3].trim().substring(list[3].trim().indexOf("=[")+2).split(", ")
            }
            person5 = {
                id: list[4].trim().substring(0, list[4].trim().indexOf("=[")),
                poker: list[4].trim().substring(list[4].trim().indexOf("=[")+2).split(", ")
            }
            person6 = {
                id: list[5].trim().substring(0, list[5].trim().indexOf("=[")),
                poker: list[5].trim().substring(list[5].trim().indexOf("=[")+2).split(", ")
            }
            // console.log(list);
            // console.log(person1);
            // console.log(person2);
            // console.log(person3);
            // console.log(person4);
            // console.log(person5);
            

            person1.poker = transfer(person1.poker)
            person2.poker = transfer(person2.poker)
            person3.poker = transfer(person3.poker)
            person4.poker = transfer(person4.poker)
            person5.poker = transfer(person5.poker)
            person6.poker = transfer(person6.poker)


            personList = [person1,person2,person3,person4,person5,person6]
            
            console.log(personList);
            lengtList = {}
            for (let i = 0; i < personList.length; i++) {
                lengtList[personList[i].id] = personList[i].poker.length
                // console.log(lengtList);
                if (sid == personList[i].id) {
                    render(personList[i].poker)
                }
                if (2147483637 === parseInt(personList[i].id)) {
                    if (personList[i].poker.length === 1) {
                        renderbottom(tempBottom)
                        tempBottom == null
                        grabFlag = true
                    } else {
                        tempBottom = personList[i].poker
                    }
                    
                }
                if (2147483636 === parseInt(personList[i].id)) { 
                    if (beforePoker === null) {
                        beforePoker = personList[i].poker;
                    }
                     
                    crrentPoker = personList[i].poker;
                    if (crrentPokerCompare(crrentPoker,beforePoker)) {
                        // launchFlag++;
                    } else {
                        beforePoker =  crrentPoker;
                        // launchFlag == 0;
                    }
                    rendercrrent(personList[i].poker)
                    // launchFlag++;
                }
                if (2147483635 === parseInt(personList[i].id)) {

                    let j = personList[i].poker[personList[i].poker.length - 1];
                    if (parseInt(personList[i].poker[2]) === parseInt(sid)) {
                        

                        document.getElementById("left").innerHTML = "张三:剩"+(lengtList[personList[i].poker[1]]===undefined ? "未知" : lengtList[personList[i].poker[1]]) +"张";
                        document.getElementById("right").innerHTML = "王五:剩"+(lengtList[personList[i].poker[0]]===undefined ? "未知" : lengtList[personList[i].poker[0]]) +"张";
                        
                        document.getElementById("userInfo").innerHTML = "李四";
                    } else if (parseInt(personList[i].poker[1]) === parseInt(sid)) {
                        document.getElementById("left").innerHTML = "王五:剩"+(lengtList[personList[i].poker[0]]===undefined ? "未知" : lengtList[personList[i].poker[0]]) +"张";
                        document.getElementById("right").innerHTML = "李四:剩"+(lengtList[personList[i].poker[2]]===undefined ? "未知" : lengtList[personList[i].poker[2]]) +"张";
                        document.getElementById("userInfo").innerHTML = "张三";
                    } else if (parseInt(personList[i].poker[0]) === parseInt(sid)) {
                        document.getElementById("left").innerHTML = "李四:剩"+ (lengtList[personList[i].poker[2]]===undefined ? "未知" : lengtList[personList[i].poker[2]]) +"张";
                        document.getElementById("right").innerHTML = "张三:剩"+ (lengtList[personList[i].poker[1]]===undefined ? "未知" : lengtList[personList[i].poker[1]]) +"张" ;
                        document.getElementById("userInfo").innerHTML = "王五";
                    }
                    
                    let h = document.getElementById("left").innerHTML;
                    let v = document.getElementById("right").innerHTML;
                    if (parseInt(personList[i].poker[0]) === parseInt(sid)) {
                        if (parseInt(j) === 2) {
                           
                            document.getElementById("left").innerHTML = "出:"+ h;
                        } else if (parseInt(j) === 1) {
    
                            document.getElementById("right").innerHTML = "出:"+v;
                        }
                    } else if (parseInt(personList[i].poker[1]) === parseInt(sid)) {
                        if (parseInt(j) === 2) {
                         
                            document.getElementById("right").innerHTML = "出:"+v;
                        }  else if (parseInt(j) === 0) {
                      
                            document.getElementById("left").innerHTML = "出:"+h;
                        }
                    } else if (parseInt(personList[i].poker[2]) === parseInt(sid)) {
                        if (parseInt(j) === 0) {
                         
                            document.getElementById("right").innerHTML = "出:"+v;
                        }  else if (parseInt(j) === 1) {
                       
                            document.getElementById("left").innerHTML = "出:"+h;
                        }
                    }
                    

                    if (parseInt(personList[i].poker[j]) === parseInt(sid) && count == 1) {
                        document.getElementById("button-lead").style.display = "block";
                    } else {
                        document.getElementById("button-lead").style.display = "none";
                    }
                    if ((parseInt(personList[i].poker[j]) === parseInt(sid)) || tempBottom === null) {
                        document.getElementById("button-list").style.display = "block";
                    } else {
                        document.getElementById("button-list").style.display = "none";
                    }
                    
                }
                if (grabFlag === true) {
                    document.getElementById("button-lead").style.display = "none";
                    // document.getElementById("button-list").style.display = "block";
                }
            }
            
        }

        function crrentPokerCompare(crrent,before) {
            if (crrent.length !== before.length) return false;
            crrent = sort(crrent);
            before = sort(before);
            for (let i = 0; i < crrent[i]; i++) {
                if (crrent[i] !== before[i]) {
                    return false;
                }
            }
            return true;
        }

        function transfer(poker) {
            for (let i = 0; i < poker.length; i++) {
                for (let j = 0; j < poker.length-i;j++) {
                    if (parseInt(poker[j]) < parseInt(poker[j+1])) {
                        var temp = poker[j];
                        poker[j] = poker[j+1];
                        poker[j+1] = temp;
                    }
                }
            }
            return poker
        }

        function render(poker) {
            document.getElementById("temp").style.display = "none";
        
            document.getElementById("poker-list").innerHTML = '';
            for (let i = 0; i < poker.length; i++) {
                let parent = document.createElement('div');
                parent.setAttribute("class", "poker")
                let node = document.createTextNode(mapping(poker[i]));
                parent.appendChild(node);
                parent.addEventListener("click", function() { 
                    if (parent.style.backgroundColor === "red") {
                        parent.style.backgroundColor = "green";
                    } else {
                        parent.style.backgroundColor = "red";
                    }
                    
                }, false);
                document.getElementById("poker-list").appendChild(parent);
            }
            document.getElementById("person").style.display = "block";
        }

        function rendercrrent(poker) {
            if (mapping(poker[0]) === undefined) return;
            document.getElementById("current-list").innerHTML = '';
            for (let i = 0; i < poker.length; i++) {
                let parent = document.createElement('div');
                parent.setAttribute("class", "poker")
                let node = document.createTextNode(mapping(poker[i]));
                parent.appendChild(node);
                parent.addEventListener("click", function() { 
                    if (parent.style.backgroundColor === "red") {
                        parent.style.backgroundColor = "green";
                        pokerList.pop(poker[i])
                    } else {
                        parent.style.backgroundColor = "red";
                        pokerList.push(poker[i])
                    }
                    
                }, false);
                document.getElementById("current-list").appendChild(parent);
            }
        }

        function renderbottom(poker) {
            if (mapping(poker[0]) === undefined) return;
            document.getElementById("bottom-list").innerHTML = '';
            for (let i = 0; i < poker.length; i++) {
                let parent = document.createElement('div');
                parent.setAttribute("class", "poker")
                let node = document.createTextNode(mapping(poker[i]));
                parent.appendChild(node);
                parent.addEventListener("click", function() { 
                    if (parent.style.backgroundColor === "red") {
                        parent.style.backgroundColor = "green";
                        pokerList.pop(poker[i])
                    } else {
                        parent.style.backgroundColor = "red";
                        pokerList.push(poker[i])
                    }
                    
                }, false);
                document.getElementById("bottom-list").appendChild(parent);
            }
        }
        
        function mapping(s) {
            if (parseInt(s) === 15) {
                return "大王";
            } else if (parseInt(s) === 14) {
                return "小王";
            } else if (parseInt(s) === 13) {
                return "2";
            } else if (parseInt(s) === 12) {
                return "A";
            } else if (parseInt(s) === 11) {
                return "K";
            } else if (parseInt(s) === 10) {
                return "Q";
            } else if (parseInt(s) === 9) {
                return "J";
            } else if (parseInt(s) === 8) {
                return "10";
            } else if (parseInt(s) === 7) {
                return "9";
            } else if (parseInt(s) === 6) {
                return "8";
            } else if (parseInt(s) === 5) {
                return "7";
            } else if (parseInt(s) === 4) {
                return "6";
            } else if (parseInt(s) === 3) {
                return "5";
            } else if (parseInt(s) === 2) {
                return "4";
            } else if (parseInt(s) === 1) {
                return "3";
            } 
        }
        function reverseMapping(s) {
            if (s === "大王") {
                return 15;
            } else if (s === "小王") {
                return 14;
            } else if (s === "2") {
                return 13;
            } else if (s=== "A") {
                return 12;
            } else if (s === "K") {
                return 11;
            } else if (s === "Q") {
                return 10;
            } else if (s === "J") {
                return  9;
            } else if (s === "10") {
                return  8;
            } else if (s === "9") {
                return 7;
            } else if (s === "8") {
                return 6;
            } else if (s === "7") {
                return  5;
            } else if (s === "6") {
                return 4;
            } else if (s === "5") {
                return 3;
            } else if (s === "4") {
                return  2;
            } else if (s === "3") {
                return  1;
            } 
        }
        function transaction(e) {
            console.log("运行")
            e.style.backgroundColor = "red";
        }

        
    </script>
</body>

</html>
<!-- 685+298
983 -->